package org.chnxi.study.springcloud.dubbo.shiro.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.chnxi.study.springcloud.dubbo.shiro.auth.entity.*;
import org.chnxi.study.springcloud.dubbo.shiro.auth.mapper.*;
import org.chnxi.study.springcloud.dubbo.shiro.auth.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2020-08-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    /**
     * 根据userId获取用户的角色列表
     * @param userId 用户ID
     * @return 用户的角色列表，可能为空，用户未分配角色
     */
    public List<Role> findRoles(Integer userId){
        if(null == userId){
            throw new IllegalArgumentException("userId为null，不能执行查询");
        }
        //从用户角色关联表查询角色ID
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq(UserRole.UID , userId);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);

        //没查询到，表示用户还没有分配角色
        if(userRoles.isEmpty()){
            return new ArrayList<>();
        }

        //从角色表根据用户的角色ID列表进行查询
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in(Role.ID, userRoles.stream().map(e -> e.getRoleId()).collect(Collectors.toList()));
        return roleMapper.selectList(wrapper);
    }

    /**
     * 根据roleId查询所有权限
     * @param roleId
     * @return
     */
    @Override
    public List<Permission> findPermissions(Integer roleId) {
        if(null == roleId){
            throw new IllegalArgumentException("roleId为null，不能执行查询");
        }
        QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq(RolePermission.ROLE_ID, roleId);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(rolePermissionQueryWrapper);

        if(rolePermissions.isEmpty()){
            return new ArrayList<>();
        }

        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.in(Permission.ID, rolePermissions.stream().map(e->e.getRoleId()).collect(Collectors.toList()));
        return permissionMapper.selectList(permissionQueryWrapper);
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public User findUserByName(String name) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(User.NAME, name);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public int assignDefaultUserRolePermission(User user) {
        int insert = userMapper.insert(user);

        if(1 != insert){
            return 0;
        }

        UserRole userRole = new UserRole();
        userRole.setUid(user.getId());
        userRole.setRoleId(2);
        int userRoleInsert = userRoleMapper.insert(userRole);

        return insert+userRoleInsert;
    }

}
